﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Forms;
using System.Data;
using System.Data.SqlClient;

namespace fideeraSoft.Paneles
{
    public class EnvasadoPanelInicio:PanelGeneral
    {
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.TableLayoutPanel tlpGral;
        private System.Windows.Forms.DateTimePicker dtFechaInicio;
        private System.Windows.Forms.GroupBox gbGral;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.Label lbId;
        private System.Windows.Forms.Button btSubmit;
        private System.Windows.Forms.Label lbFechaInicio;
        private System.Windows.Forms.Label lbObservacion;
        private System.Windows.Forms.TextBox tbObservacion;
        private System.Windows.Forms.Label lbIdEnvasado;
        
        private int posicionTablaBotones = 0;
        private Label lbCodigo;
        private TextBox tbCodigo;
        private DataGridView dgvProducciones;
        private Envasado envasado;
        private int posicionDataGrid = 0;
        private int posicionGroupBox = 0;
        private int posicionBuscarProd = 0;

        private SqlDataAdapter adapter;
        private DataSet ds;
        private TableLayoutPanel tlpBuscarProduccion;
        private TextBox tbBuscarProduccion;
        private Button btBuscarProduccion;
        private DataSet changes;


        public EnvasadoPanelInicio(Handler handler)
        {

            envasado=new Envasado();
            
           // produccion.LstMatPrima= new List<MateriaPrima>();

            this.formatearPanelGeneral(handler);

            this.InitializeComponent();
            this.formatearTableTitulo("INICIO DE ENVASADO");

            string query = "SELECT pr.ID AS [ID Producción], p.nombre AS Producto,pr.fechaInicio AS [Fecha de Inicio],pr.fechaFin AS [Fecha de Finalización], pr.observacion AS Observacion1, pr.observacionFinal AS Observación2 FROM Produccion AS pr INNER JOIN Producto AS p ON pr.fechaFin IS NOT NULL AND pr.fechaInicioEnvasado IS NULL AND pr.fechaFinEnvasado IS NULL AND p.ID=pr.producto";

            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables[0].Rows.Count <= 0)
                    MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN FINALIZADA Ó SIN ENVASAR CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else
                dgvProducciones.DataSource = ds.Tables[0];


                formatearDataGrid();
                this.formatearGroupBoxGral();
                formatearTablaBuscarProduccion();
                this.formatearTablaGral();
                formatearTablaBotones();

                this.Controls.Add(dgvProducciones);

                this.Controls.Add(gbGral);

                this.Controls.Add(tlpBotones);

            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN FINALIZADA Ó SIN ENVASAR CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }


            

        }


        #region FORMATEO

        private void formatearDataGrid()
        {            
            dgvProducciones.AutoGenerateColumns = true;
            dgvProducciones.Size = new Size(this.Size.Width - 100, 200);
            dgvProducciones.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvProducciones.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvProducciones.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

         

            dgvProducciones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvProducciones.Size.Width), InitialRealPointVertical);
        }

        private void formatearGroupBoxGral()
        {
            posicionGroupBox = this.InitialRealPointVertical + dgvProducciones.Size.Height + 30;

            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, gbGral.Size.Width), posicionGroupBox);

            //gbGral.Controls.Add(dgvProducciones);
            gbGral.Controls.Add(tlpBuscarProduccion);
            gbGral.Controls.Add(tlpGral);

        }

       


        private void formatearTablaBuscarProduccion()
        {

            posicionBuscarProd = 30;
            tlpBuscarProduccion.Location = new Point(MetodosGenerales.calcularCenterWidth(this.gbGral.Size.Width, tlpBuscarProduccion.Size.Width),posicionBuscarProd );
            
            tlpBuscarProduccion.Controls.Add(btBuscarProduccion);
            tlpBuscarProduccion.Controls.Add(tbBuscarProduccion);

        }

        private void formatearTablaGral()
        {

            tlpGral.ColumnStyles.Clear();
            for (int i = 0; i < tlpGral.ColumnCount; i++)
            {
                tlpGral.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            }

            tlpGral.RowStyles.Clear();
            for (int i = 0; i < tlpGral.RowCount; i++)
            {
                tlpGral.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
                                              

            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), posicionBuscarProd+ tlpBuscarProduccion.Size.Height+ 15);



            cargarControlesEnTabla();

            
        }

        private void cargarControlesEnTabla()
        {
            tlpGral.Controls.Add(lbId);

            lbIdEnvasado.Text = this.handler.buscarNuevoUltimoIdEnvasadoEnBD().ToString();
            tlpGral.Controls.Add(lbIdEnvasado);
            

            tlpGral.Controls.Add(lbCodigo);            
            tlpGral.Controls.Add(tbCodigo);


            tlpGral.Controls.Add(lbFechaInicio);
            dtFechaInicio.Value = System.DateTime.Now;
            tlpGral.Controls.Add(dtFechaInicio);
                        

            tlpGral.Controls.Add(lbObservacion);            
            tlpGral.Controls.Add(tbObservacion);
            
        }
     
        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

            posicionTablaBotones =  posicionGroupBox+ gbGral.Size.Height + 40;
            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBotones.BackColor = Color.LightCyan;
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }
        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.dtFechaInicio = new System.Windows.Forms.DateTimePicker();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.btVolver = new System.Windows.Forms.Button();
            this.lbId = new System.Windows.Forms.Label();
            this.btSubmit = new System.Windows.Forms.Button();
            this.lbFechaInicio = new System.Windows.Forms.Label();
            this.lbIdEnvasado = new System.Windows.Forms.Label();
            this.lbObservacion = new System.Windows.Forms.Label();
            this.tbObservacion = new System.Windows.Forms.TextBox();
            this.lbCodigo = new System.Windows.Forms.Label();
            this.tbCodigo = new System.Windows.Forms.TextBox();
            this.dgvProducciones = new System.Windows.Forms.DataGridView();
            this.tlpBuscarProduccion = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProduccion = new System.Windows.Forms.TextBox();
            this.btBuscarProduccion = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvProducciones)).BeginInit();
            this.SuspendLayout();
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSize = true;
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 100);
            this.tlpBotones.TabIndex = 0;
            // 
            // tlpGral
            // 
            this.tlpGral.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 5;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Size = new System.Drawing.Size(700, 250);
            this.tlpGral.TabIndex = 0;
            // 
            // dtFechaInicio
            // 
            this.dtFechaInicio.Location = new System.Drawing.Point(0, 0);
            this.dtFechaInicio.Name = "dtFechaInicio";
            this.dtFechaInicio.Size = new System.Drawing.Size(300, 20);
            this.dtFechaInicio.TabIndex = 0;
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(800, 380);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Inicio de Envasado";
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID Envasado";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.ForeColor = System.Drawing.Color.Black;
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Iniciar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // lbFechaInicio
            // 
            this.lbFechaInicio.AutoSize = true;
            this.lbFechaInicio.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaInicio.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaInicio.Location = new System.Drawing.Point(0, 0);
            this.lbFechaInicio.Name = "lbFechaInicio";
            this.lbFechaInicio.Size = new System.Drawing.Size(100, 23);
            this.lbFechaInicio.TabIndex = 0;
            this.lbFechaInicio.Text = "Fecha Inicio";
            this.lbFechaInicio.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdEnvasado
            // 
            this.lbIdEnvasado.AutoSize = true;
            this.lbIdEnvasado.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdEnvasado.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdEnvasado.Location = new System.Drawing.Point(0, 0);
            this.lbIdEnvasado.Name = "lbIdEnvasado";
            this.lbIdEnvasado.Size = new System.Drawing.Size(100, 23);
            this.lbIdEnvasado.TabIndex = 0;
            this.lbIdEnvasado.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbObservacion
            // 
            this.lbObservacion.AutoSize = true;
            this.lbObservacion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbObservacion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbObservacion.Location = new System.Drawing.Point(0, 0);
            this.lbObservacion.Name = "lbObservacion";
            this.lbObservacion.Size = new System.Drawing.Size(100, 23);
            this.lbObservacion.TabIndex = 0;
            this.lbObservacion.Text = "Observaciones";
            this.lbObservacion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbObservacion
            // 
            this.tbObservacion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbObservacion.Location = new System.Drawing.Point(0, 0);
            this.tbObservacion.MaxLength = 250;
            this.tbObservacion.Name = "tbObservacion";
            this.tbObservacion.Size = new System.Drawing.Size(400, 30);
            this.tbObservacion.TabIndex = 0;
            // 
            // lbCodigo
            // 
            this.lbCodigo.AutoSize = true;
            this.lbCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCodigo.Location = new System.Drawing.Point(0, 0);
            this.lbCodigo.Name = "lbCodigo";
            this.lbCodigo.Size = new System.Drawing.Size(100, 23);
            this.lbCodigo.TabIndex = 0;
            this.lbCodigo.Text = "Código";
            this.lbCodigo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCodigo
            // 
            this.tbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCodigo.Location = new System.Drawing.Point(0, 0);
            this.tbCodigo.MaxLength = 20;
            this.tbCodigo.Name = "tbCodigo";
            this.tbCodigo.Size = new System.Drawing.Size(180, 30);
            this.tbCodigo.TabIndex = 0;
            // 
            // dgvProducciones
            // 
            this.dgvProducciones.AllowUserToResizeRows = false;
            this.dgvProducciones.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.dgvProducciones.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvProducciones.Location = new System.Drawing.Point(0, 0);
            this.dgvProducciones.Name = "dgvProducciones";
            this.dgvProducciones.ReadOnly = true;
            this.dgvProducciones.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvProducciones.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvProducciones.Size = new System.Drawing.Size(240, 150);
            this.dgvProducciones.TabIndex = 0;
            this.dgvProducciones.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvProducciones_CellClick);
            // 
            // tlpBuscarProduccion
            // 
            this.tlpBuscarProduccion.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProduccion.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProduccion.ColumnCount = 2;
            this.tlpBuscarProduccion.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduccion.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduccion.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProduccion.Name = "tlpBuscarProduccion";
            this.tlpBuscarProduccion.RowCount = 1;
            this.tlpBuscarProduccion.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarProduccion.Size = new System.Drawing.Size(450, 40);
            this.tlpBuscarProduccion.TabIndex = 0;
            // 
            // tbBuscarProduccion
            // 
            this.tbBuscarProduccion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProduccion.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProduccion.MaxLength = 10;
            this.tbBuscarProduccion.Name = "tbBuscarProduccion";
            this.tbBuscarProduccion.Size = new System.Drawing.Size(180, 30);
            this.tbBuscarProduccion.TabIndex = 0;
            // 
            // btBuscarProduccion
            // 
            this.btBuscarProduccion.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProduccion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProduccion.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProduccion.ForeColor = System.Drawing.Color.Black;
            this.btBuscarProduccion.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProduccion.Name = "btBuscarProduccion";
            this.btBuscarProduccion.Size = new System.Drawing.Size(75, 40);
            this.btBuscarProduccion.TabIndex = 0;
            this.btBuscarProduccion.Text = "Buscar Producción (ID)";
            this.btBuscarProduccion.UseVisualStyleBackColor = false;
            this.btBuscarProduccion.Click += new System.EventHandler(this.btBuscarProduccion_Click);
            ((System.ComponentModel.ISupportInitialize)(this.dgvProducciones)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btSubmit_Click(object sender, EventArgs e)
        {

            try
            {

                if((dgvProducciones.Rows.Count <= 0)&&(ds.Tables[0].Rows.Count==0)||(dgvProducciones.CurrentCell==null))
                {
                    MessageBox.Show("No hay ninguna producción en la tabla.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if(validarCampos())
                {
                    MessageBox.Show("DATOS INCOMPLETOS. Asegurese de completar todos los datos necesarios.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                
                envasado.IdEnvasado = Convert.ToInt32(lbIdEnvasado.Text);
                envasado.FechaInicio = dtFechaInicio.Value.Date;

                DateTime dtFechaFinProduccion= (DateTime)dgvProducciones[3, dgvProducciones.CurrentCell.RowIndex].Value;

                if (DateTime.Compare(dtFechaFinProduccion, envasado.FechaInicio) > 0)
                {
                    MessageBox.Show("LA FECHA DE INICIO DE ENVASADO NO PUEDE SER MENOR QUE LA DEL FIN DE LA PRODUCCIÓN", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (string.IsNullOrWhiteSpace(tbCodigo.Text)) envasado.Codigo = ""; else envasado.Codigo = tbCodigo.Text;

                //envasado.Produccion = Convert.ToInt32(dgvProducciones[0, dgvProducciones.CurrentCell.RowIndex].Value.ToString());
                envasado.Produccion = (int) dgvProducciones[0, dgvProducciones.CurrentCell.RowIndex].Value;

                if (string.IsNullOrWhiteSpace(tbObservacion.Text)) envasado.Observacion = ""; else envasado.Observacion = tbObservacion.Text;
                
                DialogResult res = MessageBox.Show("Desea comenzar con el envasado:\n ID: "+envasado.IdEnvasado+" \n Produccion: "+envasado.Produccion+" \n Fecha de Inicio envasado: "+envasado.FechaInicio.Date.ToString("d")+" \n Observación: "+envasado.Observacion , "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.No)
                {
                    return;
                }
                
                handler.altaEnvasado(envasado);

                handler.updateFechaInicioEnvasadoProduccionPorId(envasado.Produccion.ToString(), envasado.FechaInicio.Date);     //ESTOY REPITIENDO INFORMACION, PODRIA SER SOLO UNA FECHA EN LA PRODUCCION
                

                MessageBox.Show("Envasado comenzado correctamente.", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                this.handler.backToPrincipal();
            }
            catch (Exception exc)
            {

                CLogger.Append("[btSubmit_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }

        }

        

        private bool validarCampos()
        {
            return ((lbIdEnvasado == null) || (string.IsNullOrWhiteSpace(lbIdEnvasado.Text)) || (dtFechaInicio.Value == null));
        }

        private void dgvProducciones_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProducciones.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btBuscarProduccion_Click(object sender, EventArgs e)
        {
            if (dgvProducciones.Rows.Count <= 0)
                return;

            dgvProducciones.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarProduccion.Text))
            {
                string searchValue = tbBuscarProduccion.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvProducciones.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvProducciones.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvProducciones.CurrentCell = row.Cells[0];
                            tbBuscarProduccion.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarProduccion.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA PRODUCCIÓN BUSCADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO PRODUCCIÓN A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }
    }
}
